#!/bin/bash -x

#
APPNAME=blogapp
BASEDIR=`pwd`/`dirname $0`
APPROOT=$BASEDIR/$APPNAME
WORKDIR=$APPROOT/_work.$$
DBFILE=$APPROOT/db/dbfile
RANDOMTXT="1234567890123456asdfghjklLKJHGFDSA_____"
TREEFROG=treefrog
TSPAWN=tspawn
PORT=8885

# Function
row_count()
{
  echo "SELECT COUNT(1) FROM blog;" | sqlite3 $DBFILE
}

# Cleanup
cleanup()
{
  # Cleanup
  cd $APPROOT
  $TREEFROG -k abort >/dev/null 2>&1

  cd $BASEDIR
  rm -rf $WORKDIR
  rm -rf $APPNAME
}

testcase()
{
  # Start Check
  cd $APPROOT
  $TREEFROG -e dev -p $PORT -d || exit
  sleep 1
  $TREEFROG -k stop || exit
  sleep 1
  $TREEFROG -e dev -p $PORT -d || exit
  sleep 1

  # GET method
  mkdir -p $WORKDIR
  cd $WORKDIR
  wget http://localhost:$PORT/blog/index || exit
  wget http://localhost:$PORT/blog/create || exit

  # POST method
  # create1
  curl --data-urlencode 'blog[title]=hello' --data-urlencode 'blog[body]=Hello world.' http://localhost:$PORT/blog/create > create1 || exit
  [ `row_count` = "1" ] || exit

  wget http://localhost:$PORT/blog/show/1 || exit

  # create2
  curl --data-urlencode 'blog[title]=Hi!' --data-urlencode 'blog[body]=Hi, all' http://localhost:$PORT/blog/create > create2 || exit
  [ `row_count` = "2" ] || exit

  wget http://localhost:$PORT/blog/show/2 || exit

  # update
  curl --data-urlencode 'blog[id]=1' --data-urlencode 'blog[title]=Hi!' --data-urlencode "blog[body]=$RANDOMTXT" http://localhost:$PORT/blog/save/1 > save1 || exit
  [ `row_count` = "2" ] || exit

  # update check
  wget http://localhost:$PORT/blog/show/1 -O show1 || exit
  if ! grep "$RANDOMTXT" show1 >/dev/null 2>&1; then exit 1; fi

  # delete
  curl  --data-urlencode 'dummy'  http://localhost:$PORT/blog/remove/1  > remove1 || exit
  [ `row_count` = "1" ] || exit

  curl  --data-urlencode 'dummy'  http://localhost:$PORT/blog/remove/2  > remove2 || exit
  [ `row_count` = "0" ] || exit

  # API
  curl -sS http://localhost:$PORT/apibook/index | jq . || exit
  curl -sS http://localhost:$PORT/apibook/get/1 | jq . || exit
  curl -sS -X POST http://localhost:$PORT/apibook/remove/1 | jq . || exit

  # Stop app
  cd $APPROOT
  $TREEFROG -k stop || exit
}

## Main ##
trap 'cleanup' 2 3 15 EXIT

# Create app
cd $BASEDIR
if [ -d "$APPNAME" ]; then
  $TREEFROG -k abort $APPNAME
  rm -rf $APPNAME
fi

$TSPAWN new $APPNAME
sqlite3 $DBFILE < create_blog_table.sql || exit

# QMake Build
cd $APPROOT
$TSPAWN --show-tables || exit
echo "n" | $TSPAWN s blog || exit
echo "n" | $TSPAWN s food || exit
echo "n" | $TSPAWN ms fuga || exit
$TSPAWN w foo || exit
$TSPAWN a book || exit
qmake -r || exit
make -j4
make || exit
$TREEFROG --show-routes || exit

testcase
cd $APPROOT
rm -f lib/*
make distclean

# CMake Build
cmake --version
mkdir build
cd build
cmake ..
cd ..
make -j4 -C build
make -C build || exit
cd $APPROOT
$TREEFROG --show-routes || exit

cd $BASEDIR
sqlite3 $DBFILE < create_blog_table.sql || exit

testcase
cd $APPROOT
rm -f lib/*
make distclean

# Vue.js & QMake
cd $APPROOT
for i in {0..5}; do sleep 1; echo "y"; done | $TSPAWN s blog || exit
for i in {0..5}; do sleep 1; echo "y"; done | $TSPAWN s food || exit
for i in {0..5}; do sleep 1; echo "y"; done | $TSPAWN ms fuga || exit
qmake -r || exit
make -j4
make || exit
$TREEFROG --show-routes || exit
$TREEFROG -k abort
rm -f lib/*
make distclean

echo
echo "Test completed."
echo "Congratulations!"

#EOF
